#include "fmod_object.h"
fmod_object::fmod_object()
{
	system = NULL;
	playlist = NULL;
	channel = NULL;
	names[0] = "./media/Nude.mp3";
	names[1] = "./media/MMM.mp3";
	names[2] = "./media/Benn_Jordan_Grant_Park.mp3";
	names[3] = "./media/Plucko.mp3";
	names[4] = "./media/13_the_flashbulb_alices_garden.mp3";
	count = 0;
}
fmod_object::~fmod_object()
{
	FMOD_RESULT result;
	result = playlist->release();
	ERRCHECK(result);
	result = system->close();
	ERRCHECK(result);
	result = system->release();
	ERRCHECK(result);
}
void fmod_object::ERRCHECK(FMOD_RESULT result)
{
	if (result != FMOD_OK)
	{
		std::stringstream ss;
		ss << "FMOD Error (" << result << ") has occured. " << FMOD_ErrorString(result);
		MessageBox(NULL,ss.str().c_str(),"FMOD ERROR",MB_OK | MB_ICONEXCLAMATION);
		exit(-1);
	}
}
void fmod_object::init()
{
	FMOD_RESULT result;
	channel = 0;
	result = FMOD::System_Create(&system); // create the system
	ERRCHECK(result);
	result = system->init(32,FMOD_INIT_NORMAL,0); // initialize the system
	ERRCHECK(result);
	MessageBox(NULL,"Creating Stream","INFO",MB_OK|MB_ICONINFORMATION);
	result = system->createStream(names[count], FMOD_HARDWARE | FMOD_2D | FMOD_LOOP_NORMAL, NULL,&playlist);
	ERRCHECK(result);
	MessageBox(NULL,"Playing Sound","INFO",MB_OK|MB_ICONINFORMATION);
	result = system->playSound(FMOD_CHANNEL_FREE,playlist,false,&channel);
	ERRCHECK(result);
}
void fmod_object::playPause()
{
	bool paused;
	FMOD_RESULT result;
	result = channel->getPaused(&paused);
	ERRCHECK(result);
	result = channel->setPaused(!paused);
	ERRCHECK(result);
}
void fmod_object::nextSong()
{
	FMOD_RESULT result;
	count++;
	count %= 5;
	result = system->createStream(names[count],FMOD_HARDWARE|FMOD_LOOP_OFF,0,&playlist);
	ERRCHECK(result);
	result = system->playSound(FMOD_CHANNEL_FREE,playlist,false,&channel);
	ERRCHECK(result);
}
void fmod_object::previousSong()
{
	FMOD_RESULT result;
	count += 4;
	count %= 5;
	result = system->createStream(names[count],FMOD_HARDWARE|FMOD_LOOP_OFF,0,&playlist);
	ERRCHECK(result);
	result = system->playSound(FMOD_CHANNEL_FREE,playlist,false,&channel);
	ERRCHECK(result);
}
void fmod_object::update()
{
	system->update();
	FMOD_RESULT result;
	if (channel) // if there is an active channel
	{
		bool         playing;
		bool         paused;
		result = channel->isPlaying(&playing);
		if ((result != FMOD_OK) && (result != FMOD_ERR_INVALID_HANDLE))
			ERRCHECK(result);
		result = channel->getPaused(&paused);
		if ((result != FMOD_OK) && (result != FMOD_ERR_INVALID_HANDLE))
			ERRCHECK(result);
		// print out prettyness
		if (!playing && !paused)
		{
			count++;
			count %= 5;
			result = system->createStream(names[count],FMOD_HARDWARE | FMOD_LOOP_OFF,0,&playlist);
			ERRCHECK(result);
			result = system->playSound(FMOD_CHANNEL_FREE,playlist,false,&channel);
			ERRCHECK(result);
		}
	}
	Sleep(10);
}
std::string fmod_object::getPlaybackInfo()
{
	FMOD_RESULT result;
	std::stringstream ss;
	unsigned int tLen; // total length of current song (ms)
	unsigned int cLen; // current length of current song (ms)
	result = playlist->getTag("TITLE",0,&tag);
	ERRCHECK(result);
	result = channel->getPosition(&cLen,FMOD_TIMEUNIT_MS);
	ERRCHECK(result);
	result = playlist->getLength(&tLen,FMOD_TIMEUNIT_MS);
	ERRCHECK(result);
	ss <<(cLen/1000/60 < 10 ? "0" : "") << cLen/1000/60 << ":" << (cLen/1000%60 < 10 ? "0" : "") << cLen/1000%60 << ":";
	ss << (cLen/10%100 < 10 ? "0" : "") << cLen/10%100 << " / " << (tLen/1000/60 < 10 ? "0" : "") << tLen/1000/60 << ":" << (tLen/1000%60 < 10 ? "0" : "");
	ss << tLen/1000%60 << ":" << (tLen/10%100 < 10 ? "0" : "") << tLen/10%100;
	return ss.str();
}